package com.ssplayer.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ssplayer.application.MyApplication;
import com.ssplayer.bean.MusicAction;
import com.ssplayer.bean.MusicMsg;
import com.ssplayer.custom.SlidingMenu;
import com.ssplayer.fragment.MusicFragment;
import com.ssplayer.fragment.MusicListFragment;
import com.ssplayer.fragment.VideoFragment;
import com.ssplayer.utils.MediaUtils;

@SuppressLint("ResourceAsColor")
public class MainActivity extends Activity implements OnClickListener {

	private FragmentTransaction transaction;
	private SharedPreferences sharedPreferences;
	private MusicListReceiver musicListReceiver;
	private LinearLayout button_menu, button_playlist, file_browse,
			button_home, button_exit;
	private FragmentManager manager;
	private MusicFragment musicFragment;
	private MusicListFragment musicListFragment;
	private VideoFragment videoFragment;
	private TextView music_tv, video_tv;
	private MyApplication myApp;
	private SlidingMenu mMenu;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		registerListReceiver();
		findAllViews();
		showMusicFragment(myApp.getCurrentlist());
	}

	private void registerListReceiver() {
		musicListReceiver = new MusicListReceiver();
		IntentFilter filter = new IntentFilter();
		filter.addAction(MusicAction.LIST_BACK);
		filter.addAction(MusicAction.LIST_DEFAULT);
		filter.addAction(MusicAction.LIST_OPEN);
		registerReceiver(musicListReceiver, filter);
	}

	private void findAllViews() {
		mMenu = (SlidingMenu) findViewById(R.id.id_menu);
		myApp = (MyApplication) getApplication();
		music_tv = (TextView) findViewById(R.id.music_tv);
		video_tv = (TextView) findViewById(R.id.video_tv);
		button_menu = (LinearLayout) findViewById(R.id.button_menu);
		button_playlist = (LinearLayout) findViewById(R.id.button_playlist);
		file_browse = (LinearLayout) findViewById(R.id.button_file_browse);
		button_home = (LinearLayout) findViewById(R.id.button_home);
		button_exit = (LinearLayout) findViewById(R.id.button_exit);
		music_tv.setOnClickListener(this);
		video_tv.setOnClickListener(this);
		button_menu.setOnClickListener(this);
		button_playlist.setOnClickListener(this);
		file_browse.setOnClickListener(this);
		button_exit.setOnClickListener(this);
		button_home.setOnClickListener(this);
	}

	/**
	 * 添加Fragment
	 */
	private void showMusicFragment(int currentlist) {
		// 获取数据并新建fragment
		musicFragment = new MusicFragment(this, MediaUtils.getMusics(this,
				currentlist));
		musicListFragment = new MusicListFragment();
		manager = getFragmentManager();
		transaction = manager.beginTransaction();
		transaction.replace(R.id.list_content, musicFragment);
		transaction.commit();
	}

	private void showVideoFragment() {
		videoFragment = new VideoFragment(this, MediaUtils.getVideos(this));
		manager = getFragmentManager();
		transaction = manager.beginTransaction();
		transaction.replace(R.id.list_content, videoFragment);
		transaction.commit();
	}

	private void showMusicListFragment() {
		musicListFragment = new MusicListFragment(this);
		manager = getFragmentManager();
		transaction = manager.beginTransaction();
		transaction.replace(R.id.list_content, musicListFragment);
		transaction.commit();

	}

	/**
	 * 单击事件
	 */
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.music_tv:
			showMusicFragment(myApp.getCurrentlist());
			music_tv.setBackgroundResource(R.color.selected);
			video_tv.setBackgroundResource(R.color.unselect);
			music_tv.setTextColor(this.getResources().getColor(R.color.white));
			video_tv.setTextColor(this.getResources().getColor(R.color.black));
			break;
		case R.id.video_tv:
			showVideoFragment();
			music_tv.setBackgroundResource(R.color.unselect);
			video_tv.setBackgroundResource(R.color.selected);
			music_tv.setTextColor(this.getResources().getColor(R.color.black));
			video_tv.setTextColor(this.getResources().getColor(R.color.white));
			break;
		case R.id.button_menu:
			mMenu.toggle();
			break;
		case R.id.button_playlist:
			mMenu.closeMenu();
			showMusicListFragment();
			break;
		case R.id.button_file_browse:
			mMenu.closeMenu();
			Intent intent = new Intent();
			intent.setClass(this, FileBrowseActivity.class);
			startActivity(intent);
			break;
		case R.id.button_home:
			mMenu.closeMenu();
			break;
		case R.id.button_exit:
			showExitDialog();
			break;
		}
	}

	@Override
	public void onBackPressed() {
		if (mMenu.isOpen) {
			mMenu.closeMenu();
		} else {
			showExitDialog();
		}
	}

	/**
	 * 弹出退出提示
	 */
	private void showExitDialog() {
		new AlertDialog.Builder(this).setIcon(R.drawable.ic_launcher)
				.setTitle("提示").setMessage("确定要退出吗")
				.setNegativeButton("确定", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						finish();
					}
				})
				.setPositiveButton("取消", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				}).create().show();
	}

	// 按菜单键弹出或隐藏菜单
	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		int keycode = event.getKeyCode();
		int action = event.getAction();
		switch (keycode) {
		case KeyEvent.KEYCODE_MENU:
			switch (action) {
			case KeyEvent.ACTION_UP:
				mMenu.toggle();
				break;
			}
			break;
		}
		return super.dispatchKeyEvent(event);
	}

	/**
	 * 退出时清除一些数据
	 */
	@Override
	public void onDestroy() {
		myApp.setMplay_status(MusicMsg.STATUS_STOP);
		myApp.setMvideo_playtime(0);
		Intent intent = new Intent();
		intent.setAction(MusicAction.MUSIC_SERVICE);
		stopService(intent);
		super.onDestroy();
	}

	public class MusicListReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			Intent stopservice = new Intent();
			stopservice.setAction(MusicAction.MUSIC_SERVICE);
			switch (action) {
			case MusicAction.LIST_BACK:
				showMusicFragment(myApp.getCurrentlist());
				break;
			case MusicAction.LIST_OPEN:
				// 先停止服务
				context.stopService(stopservice);
				// 打开新的列表
				int currentlist = intent.getIntExtra("listID", -1);
				myApp.setCurrentlist(currentlist);
				showMusicFragment(currentlist);
				break;
			case MusicAction.LIST_DEFAULT:
				// 先停止服务，在服务重启时获取新列表
				context.stopService(stopservice);
				myApp.setCurrentlist(-1);
				showMusicFragment(-1);
				break;

			default:
				break;
			}
		}
	}

}
